home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 1 (Walnut Creek)
/
Aminet - June 1993 [Walnut Creek].iso
/
aminet
/
dev
/
lang
/
perl4_035_v010.lzh
/
perl4.035
/
cscript
/
macros.h
< prev
next >
Wrap
C/C++ Source or Header
|
1992-09-19
|
8KB
|
212 lines
#ifndef MACROS_H
#define MACROS_H
/*****************************************************************************
macros.h
DESCRIPTION: Useful macros for C programs.
AUTHOR: Kent Dalton --- 8/1/91
Copyright 1991 by Kent Dalton
*****************************************************************************/
#include <ctype.h>
/******************************* MACROS *************************************/
/*** allocate a number of elements of specified type ****/
#define vCALLOCN(ptr, type, number) { \
if((ptr=(type *)calloc(number, sizeof(type))) == NULL) { \
fprintf(stderr, "**** Error - memory allocation failed.\n"); \
exit(1); \
} \
}
/*** add an environment var to a path string ***/
#define vGETENV(s,e,d) { \
char *p; \
if((p = (char *)getenv((char *)e)) == NULL) { \
if((p=(char *)malloc(strlen(d)+1)) == NULL) { \
fprintf(stderr, "**** Error - memory allocation failed.\n"); \
exit(1); \
} \
strcpy(p,d); \
} \
strcpy(s,p); \
free(p); \
}
/*** Save string to a pointer ****/
#define vSAVESTR(dest, source) { \
if((dest = (char *)malloc(strlen(source)+1)) == NULL) { \
fprintf(stderr, "**** Error - memory allocation failed.\n"); \
exit(1); \
} \
strcpy(dest,source); \
}
#define vSTRACPY(dest, source) { \
if((dest = (char *)malloc(strlen(source)+1)) == NULL) { \
fprintf(stderr, "**** Error - memory allocation failed.\n"); \
exit(1); \
} \
strcpy(dest,source); \
}
/*** Open file with error checking ****/
#define vOPENFILE(ptr, name, mode) { \
if((ptr = fopen(name, mode)) == NULL) { \
fprintf(stderr, "**** Error - unable to open file: %s\n", name); \
exit(1); \
} \
}
/*** Get a piece of data from a string in a file ***/
#define vGETS_DATA(file, data, format) { \
char acGarbageBuf[BUFSIZ]; \
int iIndex; \
if(fgets(acGarbageBuf, BUFSIZ, file == NULL)) { \
fprintf(stderr, "**** Error - unable to get string from file.\n"); \
exit(1); \
} \
iIndex = strspn(acGarbageBuf, " \n\t\f\r"); \
if(acGarbageBuf[iIndex] == '\0') { \
fprintf(stderr, "**** Error - unexpected empty string in file\n"); } \
else sscanf(acGarbageBuf, format, &(data)); \
}
/*** Get a piece of data from a string in a file ***/
#define vGETS_DDEF(file, data, format, default) { \
char acGarbageBuf[BUFSIZ]; \
int iIndex; \
if(fgets(acGarbageBuf, BUFSIZ, file == NULL)) { \
fprintf(stderr, "**** Error - unable to get string from file.\n"); \
exit(1); \
} \
iIndex = strspn(acGarbageBuf, " \n\t\f\r"); \
if(acGarbageBuf[i] == '\0') data = default; \
else sscanf(acGarbageBuf, format, &(data)); \
}
#define vERROR(class, msg, exit_flag, status) { \
fprintf(stderr, "%s: **** %s - %s\n", PROGRAM, class, msg); \
if(exit_flag) exit(status); \
}
/*** reverse strip - strip character from end of string ***/
#define vREV_STRIP(s, c) { \
int bar; \
for(bar = strlen(s)-1; bar >= 0; bar--) \
if(s[bar] == c) s[bar]='\0'; \
}
/*** lowercase string ****/
#define vMAKELOWER(s) { \
int foo; \
for(foo=0; s[foo] != '\0'; foo++) { \
if(isupper(s[foo])) s[foo]=tolower(s[foo]); \
} \
}
/*** lowercase string ****/
#define vMAKEUPPER(s) { \
int foo; \
for(foo=0; s[foo] != '\0'; foo++) { \
if(islower(s[foo])) s[foo]=toupper(s[foo]); \
} \
}
#define vDCONNECT(ptOne, ptTwo) { \
ptOne->ptNext = ptTwo; \
ptTwo->ptPrev = ptOne; \
ptTwo->ptNext = NULL; \
}
#define vSCONNECT(ptOne, ptTwo) { \
ptOne->ptNext = ptTwo; \
ptTwo->ptNext = NULL; \
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* The ASSERT macro is used for testing internal assertions within code -- it is
* intended to be used when something is known to be true but Murphy's law
* behooves us to make sure. It is fairly efficient and doesn't require
* the developer to add gazillions of printf()s and so forth.
*
* If the macro has been defined then we don't bother with it here...
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
#ifndef ASSERT
#ifdef __STDC__
#ifdef unix
/********** ANSI C *********** UNIX **********/
#define ASSERT(exp) \
{ \
if (!(exp)) \
{ \
fprintf(stderr, "Internal assertion failure " #exp " at line %d" \
" in file \"" __FILE__ "\".\n", __LINE__); \
fputs("This signifies an internal error.\n", stderr); \
fputs("Contact NCR immediately for assistance with this problem.\n", stderr); \
fputs("Dumping core via abort()...\n", stderr); \
abort(); \
} \
}
#else
/********** ANSI C *********** No UNIX **********/
#define ASSERT(exp) \
{ \
if (!(exp)) \
{ \
fprintf(stderr, "Internal assertion failure " #exp " at line %d" \
" in file \"" __FILE__ "\".\n", __LINE__); \
fputs("This signifies an internal error.\n", stderr); \
fputs("Contact NCR immediately for assistance with this problem.\n", stderr); \
} \
}
#endif /* unix or not */
#else /* not STDC */
/********** Not ANSI C *********** UNIX **********/
#ifdef unix
#define ASSERT(exp) \
{ \
if (!(exp)) \
{ \
fprintf(stderr, "Internal assertion failure \"exp\" at line %d in file \"%s\".\n", \
__LINE__, __FILE__); \
fputs("This signifies an internal error.\n", stderr); \
fputs("Contact NCR immediately for assistance with this problem.\n", stderr); \
fputs("Dumping core via abort()...\n", stderr); \
abort(); \
} \
}
#else
/********** Not ANSI C *********** Not UNIX **********/
#define ASSERT(exp) \
{ \
if (!(exp)) \
{ \
fprintf(stderr, "Internal assertion failure \"exp\" at line %d in file \"%s\".\n", \
__LINE__, __FILE__); \
fputs("This signifies an internal error.\n", stderr); \
fputs("Contact NCR immediately for assistance with this problem.\n", stderr); \
} \
}
#endif /* UNIX or not */
#endif /* not STDC */
#endif /* ASSERT */
#endif